package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author Cc
 * @version 1.0
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/admin")
public class AdminAdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminAdminController.class);

    @Autowired
    private DtsAdminService adminService;

    /**
     * 系统管理 - 管理员查询列表
     *
     * @return
     */
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String username, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {

        IPage<DtsAdmin> iPage = adminService.findListByQuery(username, sort, order, page, limit);
        HashMap map = new HashMap();
        map.put("total", iPage.getTotal());
        map.put("items", iPage.getRecords());


        return ResponseUtil.ok(map);
    }


    /**
     * 添加管理员  接口地址 : http://localhost:8083/admin/admin/create
     *
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin) {

        admin = adminService.create(admin);
        return ResponseUtil.ok(admin);
    }

    /**
     * 修改管理员信息
     *
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "管理员管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin) {
        Integer anotherAdminId = admin.getId();
        if (anotherAdminId == null) {
            return ResponseUtil.badArgument();
        }

        String rawPassword = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        admin.setPassword(encodedPassword);


        if (adminService.updateById(admin) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(admin);
    }


    /**
     * 删除管理员账户  应该把阿里云中的图片一起删除掉
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin) {
        Integer anotherAdminId = admin.getId();
        if (anotherAdminId == null) {
            return ResponseUtil.badArgument();
        }
        adminService.deleteById(anotherAdminId);

        return ResponseUtil.ok();
    }
}
